package cn.edu.ujn.multiple.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.multiple.dao.CustomerMapper;
import cn.edu.ujn.multiple.model.Customer;
import cn.edu.ujn.multiple.service.ICustomerService;
import cn.edu.ujn.multiple.util.Page;
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerMapper customerDao;
	
	
	@Override
	public Customer findByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		Customer customer = customerDao.selectByPrimaryKey(custId);
		return customer;
	}
	
	//动态查询顾客条目
	@Override
	public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource,
			String custIndustry, String custLevel) {
		// TODO Auto-generated method stub
		Customer customer = new Customer();
		if(StringUtils.isNotBlank(custName)) {
			customer.setCustName(custName);
		}
		if(StringUtils.isNotBlank(custSource)) {
			customer.setCustSource(custSource);
		}
		if(StringUtils.isNotBlank(custIndustry)) {
			customer.setCustIndustry(custIndustry);
		}
		if(StringUtils.isNotBlank(custLevel)) {
			customer.setCustLevel(custLevel);
		}
		customer.setStart((page-1)*rows);
		customer.setRows(rows);
		List<Customer> customers = customerDao.selectCustomerList(customer);
		for(int i=0;i<customers.size();i++) {
			String source = customers.get(i).getSourceDict().getDictItemName();
			String industry = customers.get(i).getIndustryDict().getDictItemName();
			String level = customers.get(i).getLevelDict().getDictItemName();
			customers.get(i).setCustSource(source);
			customers.get(i).setCustIndustry(industry);
			customers.get(i).setCustLevel(level);
		}
		Integer count = customerDao.selectCustomerListCount(customer);
		Page<Customer> result = new Page<Customer>();
		result.setPage(page);
		result.setRows(customers);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}

	@Override
	public Customer insert(Customer customer) {
		// TODO Auto-generated method stub
		int i = customerDao.insertSelective(customer);
		if(i>0) {
			return customer;
		} else {
			return null;
		}
	}

	@Override
	public Customer update(Customer customer) {
		// TODO Auto-generated method stub
		int i = customerDao.updateByPrimaryKeySelective(customer);
		if(i>0) {
			return customer;
		} else {
			return null;
		}
	}

	@Override
	public Integer delete(Integer custId) {
		// TODO Auto-generated method stub
		int i = customerDao.deleteByPrimaryKey(custId);
		return i;
	}

}